<template>
  <div class="page-class">
    <Breadcrumb />
    <div class="cnt-class">
      <div class="query">
        <a-row>
          <a-col :span="6">
            <a-input-search
              v-model.trim="userName"
              :maxLength="50"
              allowClear
              placeholder="请输入用户名或手机号"
              @search="onSearch"
            >
              <a-button slot="enterButton" type="primary"> 搜索 </a-button>
            </a-input-search>
          </a-col>
        </a-row>
        <!-- 筛选列表 -->
        <Collapse :opend="opend" @changOpend="changOpend">
          <a-row>
            <a-col :span="24">
              <span class="collapse-label">类型：</span>
              <a-radio-group v-model="query.type">
                <a-radio
                  v-for="(node, index) in typeList"
                  :value="node.value"
                  :key="index"
                >
                  {{ node.name }}
                </a-radio>
              </a-radio-group>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24">
              <a-button type="primary" @click="getList(2)">查询</a-button>
              <a-button type="default" @click="handleRest">重置</a-button>
            </a-col>
          </a-row>
        </Collapse>
      </div>
      <div class="table">
        <BaseTable
          ref="table"
          rowKey="id"
          :scroll="{ y: y }"
          :loading="loading"
          :columns="columns"
          :dataSource="list"
          :hideSelect="true"
          @change="getList"
        ></BaseTable>
      </div>
    </div>
  </div>
</template>
<script>
import BaseTable from "@/components/BaseTable";
import Collapse from "@/components/Collapse";
import httpApi from "@/api/log";
import { typeList } from "@/utils";

export default {
  components: {
    BaseTable,
    Collapse
  },
  data() {
    return {
      loading: false,
      opend: true,
      query: {
        type: undefined
      },
      typeList,
      time: -1,
      userName: undefined,
      list: [],
      columns: [
        {
          title: "编号",
          dataIndex: "userNo",
          ellipsis: true
        },
        {
          title: "用户名",
          dataIndex: "userName",
          ellipsis: true
        },
        {
          title: "类型",
          dataIndex: "type",
          ellipsis: true
        },
        {
          title: "手机号",
          dataIndex: "mobile",
          ellipsis: true
        },
        {
          title: "访问时间",
          dataIndex: "createTime",
          ellipsis: true
        }
      ]
    };
  },
  mounted() {
    this.getList();
  },
  computed: {
    y() {
      return this.opend ? 380 : 480;
    }
  },
  methods: {
    changOpend(opend) {
      this.opend = opend;
    },
    handleRest() {
      // 重置
      this.query.type = undefined;
    },
    onSearch() {
      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.getList(1);
      }, 600);
    },
    getList(current) {
      this.loading = true;
      if (current === 1) {
        this.$refs.table.pageInfo.current = 1;
      }
      httpApi
        .getList({
          userName: this.userName,
          pageNo: this.$refs.table.pageInfo.current,
          pageSize: this.$refs.table.pageInfo.pageSize
        })
        .then((res) => {
          if (res.retcode === 0) {
            this.list = res.data.records || [];
            if (this.$refs.table) {
              this.$refs.table.pageInfo.total = res.data.total;
            }
          } else {
            this.$message.error(res.msg);
          }
          this.loading = false;
        })
        .catch((err) => {
          this.$message.error(err.msg);
          this.loading = false;
        });
    }
  }
};
</script>
